package com.hnkywl.sts.action.jsgl;

import com.hnkywl.sts.action.flow.assign.RegectTaskSingleBackWayCmd;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.cggl.Zjbcd;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.jsgl.ZjbcdService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.KhService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-6-8
 * Time: 上午1:03
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jsgl/zjbcd")
public class ZjbcdAction extends SimpleActionSupport {
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //采购结算单ID
    private Long cgjsdId;
    //物流结算单ID
    private Long wljsdId;

    private Long gysId;

    private Long sbgsId;

    private Long sbbmId;

    private Long sbrId;

    private String wzbm;
    private String wzmc;

    private Long zjbcdId;

    private Long khId;

    private Zjbcd zjbcd;

    private Gsjg gys;

    private List<Gncz> gnczList;

    private ZjbcdService zjbcdService;

    private KhService khService;

    private GnczService gnczService;

    private FlowService flowService;

    private ProcessEngine processEngine;

    private TaskService taskService;

    private ExecutionService executionService;

    private HistoryService historyService;

    private String result;

    private String taskId;

    private String content;

    private String processInstanceId;

    private String executionId;

    private GsjgDao gsjgDao;

    @Action("zjbcd")
    public String zjbcd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        gys = gsjgDao.get(gysId);
        return SUCCESS;
    }

    @Action("zjbcdSearchList")
    public void zjbcdSearchList() {
        this.renderJson(this.zjbcdService.findZjbcdSearch(this.cgjsdId, this.page, this.rows));
    }

    @Action("zjbcdDetailList")
    public void zjbcdDetailList() {
        this.renderJson(this.zjbcdService.findCgjsddmxSearch(cgjsdId));
    }

    @Action("zjbcdSearchWlList")
    public void zjbcdSearchListWl() {
        this.renderJson(this.zjbcdService.findZjbcdSearchWl(this.wljsdId, this.page, this.rows));
    }

    @Action("zjbcdList")
    public void zjbcdList() {
        this.renderJson(this.zjbcdService.findZjbcdList(this.wzbm, this.wzmc, this.gysId, this.sbgsId, this.sbbmId, this.sbrId, this.page, this.rows));
    }

    @Action("zjpcdList")
    public void zjpcdList() {
        this.renderJson(this.zjbcdService.findZjpcdList(this.gysId, this.sbgsId, this.sbbmId));
    }

    @Action("kcsearch")
    public String kcsearch() {
        return SUCCESS;
    }

    @Action("useZjbcd")
    public void useZjbcd() {
        try {
            this.zjbcdService.useZjbcd(this.zjbcdId, khId);
            renderText("{\"statusCode\":\"200\", \"message\":\"资金补差单使用成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("zjbcd_detail")
    public String jkdDetail() {
        zjbcd = zjbcdService.findZjbcd(zjbcdId);
        return SUCCESS;
    }

    @Action("zjbcdByProcessInstance")
    public void zjbcdByProcessInstance() {
        Zjbcd zjbcd;
        String processInstanceId = StringUtils.EMPTY;
        Execution execution = this.executionService.findExecutionById(executionId);
        if (execution != null) {
            zjbcd = this.zjbcdService.findZjbcdDetailByProcessInstance(execution.getProcessInstance().getId());
            processInstanceId = execution.getProcessInstance().getId();
        } else {
            zjbcd = this.zjbcdService.findZjbcdDetailByProcessInstance(executionId);
            processInstanceId = executionId;
        }
        String xyJson = "{'id':'" + zjbcd.getId() + "','type':'ZJBCD','businessNo':'" + zjbcd.getId() + "','processInstanceId':'" + processInstanceId + "'}";
        this.renderText(xyJson);
    }

    /**
     * 方法名:     提交资金补差单
     * 功能描述:      提交资金补差单审核
     * 输入参数:    <@param zjbcdId 资金补差单ID>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("commit_zjbcd")
    public void commitZjbcd() {
        try {
            Zjbcd zjbcd = this.zjbcdService.findZjbcd(zjbcdId);
            if (this.flowService.checkFlowStart("资金补差单流程")) {
                String contain = StringUtils.EMPTY;
                String processInstanceId = this.flowService.regTask(zjbcd.getProcessInstanceId(), "资金补差单流程", contain);
                zjbcd.setProcessInstanceId(processInstanceId);
                zjbcd.setZt("ytj");
            } else
                zjbcd.setZt("ysp");
            this.zjbcdService.saveZjbcd(zjbcd);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交资金补差单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action(value = "examineTask")
    public void examineTask() {
        if (this.result.equals("审核")) {
            Map<String, Object> var = new HashMap<String, Object>();
            var.put("owner", YgService.getCurrentYg().getId());
            var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
            var.put("result", "审核通过");
            var.put("content", content);
            var.put("time", new Date());
            Task currentTask = this.taskService.getTask(taskId);
            String executionId = currentTask.getExecutionId();
            executionService.createVariables(executionId, var, true);
            var.put("status", result);

            Execution execution = this.executionService.findExecutionById(executionId);
            Zjbcd zjbcd = zjbcdService.findZjbcdDetailByProcessInstance(execution.getProcessInstance().getId());
            this.taskService.completeTask(taskId, var);

            if (zjbcd.getZt().equals("ybc") || zjbcd.getZt().equals("")) {
                zjbcd.setZt("ytj");
                zjbcdService.saveZjbcd(zjbcd);
            }
        } else {
            String result = processEngine.execute(new RegectTaskSingleBackWayCmd(taskId, content));
            if (result.equals("change")) {
                //修改状态  ytj--->ybc
                String executionId = this.historyService.createHistoryTaskQuery().taskId(taskId).uniqueResult().getExecutionId();
                Execution execution = this.executionService.findExecutionById(executionId);
                Zjbcd zjbcd = zjbcdService.findZjbcdDetailByProcessInstance(execution.getProcessInstance().getId());
                zjbcd.setZt("ybc");
                zjbcdService.saveZjbcd(zjbcd);
            }
        }
        this.renderText("{\"statusCode\":\"200\", \"message\":\"审核流程成功\"}");
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getCgjsdId() {
        return cgjsdId;
    }

    public void setCgjsdId(Long cgjsdId) {
        this.cgjsdId = cgjsdId;
    }

    public Long getWljsdId() {
        return wljsdId;
    }

    public void setWljsdId(Long wljsdId) {
        this.wljsdId = wljsdId;
    }

    public Long getGysId() {
        return gysId;
    }

    public void setGysId(Long gysId) {
        this.gysId = gysId;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public Long getSbbmId() {
        return sbbmId;
    }

    public void setSbbmId(Long sbbmId) {
        this.sbbmId = sbbmId;
    }

    public Long getSbrId() {
        return sbrId;
    }

    public void setSbrId(Long sbrId) {
        this.sbrId = sbrId;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public String getWzmc() {
        return wzmc;
    }

    public void setWzmc(String wzmc) {
        this.wzmc = wzmc;
    }

    public Long getZjbcdId() {
        return zjbcdId;
    }

    public void setZjbcdId(Long zjbcdId) {
        this.zjbcdId = zjbcdId;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Zjbcd getZjbcd() {
        return zjbcd;
    }

    public void setZjbcd(Zjbcd zjbcd) {
        this.zjbcd = zjbcd;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public String getExecutionId() {
        return executionId;
    }

    public Gsjg getGys() {
        return gys;
    }

    public void setGys(Gsjg gys) {
        this.gys = gys;
    }

    public void setExecutionId(String executionId) {
        this.executionId = executionId;
    }

    @Autowired
    public void setZjbcdService(ZjbcdService zjbcdService) {
        this.zjbcdService = zjbcdService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setKhService(KhService khService) {
        this.khService = khService;
    }

    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }

    @Autowired
    public void setExecutionService(@Qualifier("executionService") ExecutionService executionService) {
        this.executionService = executionService;
    }

    @Autowired
    public void setTaskService(@Qualifier("taskService") TaskService taskService) {
        this.taskService = taskService;
    }

    @Autowired
    public void setProcessEngine(@Qualifier("processEngine") ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    @Autowired
    public void setHistoryService(@Qualifier("historyService") HistoryService historyService) {
        this.historyService = historyService;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
